package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCall;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  中心话务分配Action
 * @author yangzj
 * @datetime 2014年10月14日,上午10:39:10
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallAllocateCenterAction extends PublicAction implements ModelDriven<MedicalCall>{
	
	private static final long serialVersionUID = 6349279192012513543L;
	
	private MedicalCall call = new MedicalCall();
	
	/**
	 * @desc 页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月24日,下午10:03:41
	 */
	@Action(value = "call_allocate_center_init", results = {@Result(location = "call_allocate_center_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询未分配话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月15日,下午4:46:43
	 */
	@Action(value = "call_allocate_center_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getCallCenterManagerService().findCallByPage(call, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 新增任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月20日,下午3:50:05
	 */
	@Action(value = "call_allocate_add", results = {@Result(location = "call_allocate_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 打开批量导入页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月15日,下午10:19:19
	 */
	@Action(value = "call_allocate_import_add", results = {@Result(location = "call_allocate_import_add.jsp")})
	public String importAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 导入保存
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月22日,上午9:53:43
	 */
	@Action(value = "call_allocate_import_save")
	public String importSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		File file = this.getImportFile(request);
		try {
			this.baseAllService.getCallManagerService().importSave(admin, file);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 查询话务组下、话务员集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月28日,上午9:50:14
	 */
	@Action(value = "call_allocate_center_find_user")
	public String findUserList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getCallCenterManagerService().findUserList(call.getGroupId());
		formatJSON(response, list);
		return null;
	}

	/**
	 * @desc 批量分配到话务员
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月28日,上午11:00:03
	 */
	@Action(value = "call_allocate_center_user")
	public String allocateUser() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId()) && StringUtils.isNotBlank(call.getHandleUserId())) {
			boolean flag = this.baseAllService.getCallCenterManagerService().callAllocate(call.getId(), call.getHandleUserId());
			if(flag) {
				map.put("success", "true");
			} else {
				map.put("success", "error");
			}
		}else {
			map.put("success", "false");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询当前用户所在中心下组的集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月27日,下午2:08:53
	 */
	@Action(value = "call_allocate_center_find_group")
	public String findGroupList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getCallCenterManagerService().findGroupList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 批量分配到组
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月27日,下午9:05:16
	 */
	@Action(value = "call_allocate_center_group")
	public String allocateGroup() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId()) && StringUtils.isNotBlank(call.getGroupId())) {
			boolean flag = this.baseAllService.getCallCenterManagerService().callAllocateGroup(call.getId(), call.getGroupId());
			if(flag) {
				map.put("success", "true");
			} else {
				map.put("success", "error");
			}
		}else {
			map.put("success", "false");
		}
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 批量修改类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月23日,下午2:07:46
	 */
	@Action(value = "call_allocate_center_cat")
	public String editCategory(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId()) && StringUtils.isNotBlank(call.getCategory())){
			this.baseAllService.getCallCenterManagerService().editCallCategory(call.getId(), call.getCategory());
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量修改任务状态
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月25日,下午4:59:10
	 */
	@Action(value = "call_allocate_center_sta")
	public String editStatus() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId()) && StringUtils.isNotBlank(call.getPocessingStatus())) {
			this.baseAllService.getCallCenterManagerService().editCallStatus(call.getId(), call.getPocessingStatus());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月19日,下午7:25:29
	 */
	@Action(value = "call_allocate_center_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(call.getId())){
			this.baseAllService.getCallCenterManagerService().delCall(call);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 导出当前页
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午2:42:06
	 */
	@Action(value = "call_allocate_center_export_cur")
	public String exportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心未分配话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			if(StringUtils.isNotBlank(call.getId())){
				Workbook workbook = this.baseAllService.getCallCenterManagerService().exportCurrent(call.getId());
				workbook.write(toClient);
			}
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出全部
	 * @return null
	 * @author liy
	 * @datetime 2015年1月4日,下午3:30:19
	 */
	@Action(value = "call_allocate_center_export_all")
	public String unaExportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心未分配话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getCallCenterManagerService().exportAll(admin,this.call);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public MedicalCall getModel() {
		return call;
	}

}
